<template>
  <div>
    <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
        <el-row>
            <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="pageData.query">
                    <el-button slot="append" icon="el-icon-search" @click="getOrders()"></el-button>
                </el-input>
            </el-col>
        </el-row>

        <el-table
            :data="orderList"
            stripe
            border
            style="width: 100%">
            <el-table-column
            type="index"
            width="50"
            label="#">
            </el-table-column>
            <el-table-column
            label="订单编号"
            prop="order_number"
            min-width="400">
            </el-table-column>
            <el-table-column
            label="订单价格"
            prop="order_price"
            width="80">
            </el-table-column>
            <el-table-column
            label="是否付款"
            prop="pay_status"
            width="80">
                <template scope="{row}">
                    <el-tag :disable-transitions="true" type="success" v-show="row.pay_status === '1'">已付款</el-tag>
                    <el-tag :disable-transitions="true" type="danger" v-show="row.pay_status === '0'">未付款</el-tag>
                </template>
            </el-table-column>
            <el-table-column
            label="是否发货"
            prop="is_send"
            width="80">
            </el-table-column>
            <el-table-column
            label="下单时间"
            prop="create_time"
            min-width="200">
            </el-table-column>
            <el-table-column
            label="操作"
            width="170">
                <el-tooltip class="item" effect="dark" content="修改地址" placement="top" :enterable="false">
                    <el-button 
                    type="primary" 
                    size="mini" 
                    icon="el-icon-edit"
                    @click="setAddressDialog">
                    </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="物流信息" placement="top" :enterable="false">
                    <el-button type="success" size="mini" icon="el-icon-location" @click="showLogistics"></el-button>
                </el-tooltip>
            </el-table-column>

        </el-table>

        <div>
            <el-pagination
            :current-page="pageData.pagenum"
            :page-size="pageData.pagesize"
            :page-sizes="[1,3,5,10]"
            @size-change="sizeChange"
            @current-change="currentChange"
            layout="total, prev, pager, next, jumper,sizes"
            :total="total">
            </el-pagination>
        </div>
    </el-card>

    <!-- 修改地址dialog -->
    <el-dialog
    title="修改地址"
    :visible.sync="addressDialogVisible"
    :modal-append-to-body="false"
    width="50%"
    @close="closeFormData">
        <el-form 
        ref="cityformRef" 
        :model="editCityFormData" 
        label-width="100px"
        :rules="editCityFormRules">
            <el-form-item label="省市区/县" prop="address">
                <el-cascader
                :options="cityData"
                v-model="editCityFormData.address"
                @change="cityChange"
                :props="cityProps">
                </el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="detailed">
                <el-input v-model="editCityFormData.detailed"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer">
            <el-button @click="addressDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addressDialogCommit">确 定</el-button>
        </span>
    </el-dialog>

    <!-- 展示物流信息进度dialog -->
    <el-dialog
        title="物流进度"
        :visible.sync="logisticsDialogVisible"
        :modal-append-to-body="false"
        width="50%">
        <el-timeline :reverse="true">
            <el-timeline-item
                v-for="(activity, index) in logisticsData"
                :key="index"
                :timestamp="activity.time">
                {{activity.context}}
            </el-timeline-item>
        </el-timeline>
        <div slot="footer">
            <el-button @click="logisticsDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="logisticsDialogVisible = false">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js';

export default {
    name:'Order',
    data(){
        return {
            // 分页的相关数据
            pageData:{
                query:'',
                pagenum:1,
                pagesize:5,
            },
            // 总数据条数
            total:0,
            // 获取到的数据列表
            orderList:[],

            // 控制修改地址dialog显示与隐藏
            addressDialogVisible:false,
            // 修改地址的表单数据
            editCityFormData:{
                address:[],
                detailed:''
            },
            // 修改地址的表单数据的校验对象
            editCityFormRules:{
                address:{required: true, message:'请输入省市区', trigger: 'blur'},
                detailed:{required: true, message:'请输入详细地址', trigger: 'blur'}
            },
            // 省市区级联选择器数据
            cityData,
            // 省市区级联选择器的配置规则
            cityProps:{
                label:'label',
                value:'value',
                children:'children',
            },

            // 控制展示物流信息dialog对话框的显示与隐藏
            logisticsDialogVisible:false,
            // 物流信息的数据
            logisticsData:[],
        }
    },
    methods: {
        // 获取商品订单列表
        async getOrders(){
            const {data:res} = await this.$axios.get('orders',{
                params:this.pageData
            });
            if(res.meta.status!==200) return this.$message.error('获取商品订单列表失败');
            
            res.data.goods.forEach(item=>{
                item.create_time = this.$transformDate(item.create_time)
            })

            this.orderList = res.data.goods;
            this.total = res.data.total;

            console.log(res);
        },
        // 当前展示多少条数据发生改变时
        sizeChange(size){
            this.pageData.pagesize = size;
            this.getOrders();
        },
        // 当前页数发生改变时
        currentChange(num){
            this.pageData.pagenum = num;
            this.getOrders()
        },

        // 修改地址编辑按钮触发
        setAddressDialog(){
            
            this.addressDialogVisible = true;
            
        },
        // 修改地址dialog对话框确认提交按钮触发
        addressDialogCommit(){
            this.$refs.cityformRef.validate(valid=>{
                if(!valid) return this.$message.error('请填写必要的表单');
                console.log("校验通过，可以开始发请求");
                this.addressDialogVisible = false;
            })
        },
        // 修改地址中级联选择器的选择地变化
        cityChange(newCityAddress){
            this.editCityFormData.address = newCityAddress;
        },
        // 修改地址dialog对话框关闭时触发
        closeFormData(){
            this.$refs.cityformRef.resetFields();
        },
        
        // 点击物流信息按钮后事件
        async showLogistics(){
            // const {data:res} = await this.$axios.get(`/kuaidi/1106975712662`);
            // console.log(res);
            // if(res.meta.status!==200) return this.$message.error('物流信息获取失败')
            // this.logisticsData = res.data;
            // 接口出问题所以获取不会成功，可利用死数据暂时过渡
            // const res = await import('./logisticsData.js');
            // console.log(res);

            const {default:res} = await import('./logisticsData.js');
            this.logisticsData = res;
            console.log(this.logisticsData);
            this.logisticsDialogVisible = true;
        },
        
    },
    mounted() {
        this.getOrders();
    },
}
</script>

<style lang="less" scoped>
@import url('../../plugins/timeline/timeline.css');
@import url('../../plugins/timeline-item/timeline-item.css');

.el-cascader{
    width: 100%;
}
</style>